<template>
  <div>
    <basic-container>
      <avue-crud
        ref="scopeTree"
        :data="tableData"
        :option="option"
        :page="page"
        :destroy-on-close="true"
        @row-save="rowSave"
        @refresh-change="searchHandler"
        @row-del="btnDelete"
        @row-update="rowUpdate"
        @on-load="onLoad"
      >
      </avue-crud>
    </basic-container>
  </div>
</template>
<script>
import { addTerm, getTerm, deleteTerm, updateTerm } from "@/api/basicData/term";
export default {
  name: "",
  data() {
    return {
      page: {
        pageSize: 10,
        total: 10
      },
      searchForm: {
        currentPage: 1,
        pageSizes: 10,
        term: "",
        year: ""
      },
      tableData: [],
      option: {
        border: true,
        index: true,
        indexLabel: "序号",
        stripe: true,
        menuAlign: "center",
        editBtn: this.getPermission("term_update"),
        delBtn: this.getPermission("term_delete"),
        addBtn: this.getPermission("term_add"),
        viewBtn: this.getPermission("term_get"),
        searchMenuSpan: 6,
        align: "center",
        column: [
          {
            label: "学年",
            prop: "year",
            span: 12,
            rules: [
              {
                required: true,
                message: "学年不能为空",
                trigger: "blur"
              }
            ]
          },
          {
            label: "学期名称",
            prop: "term",
            span: 12,
            rules: [
              {
                required: true,
                message: "学期不能为空",
                trigger: "blur"
              }
            ]
          },
          {
            label: "是否默认学期",
            prop: "isCurrent",
            span: 12,
            type: "select",
             labelWidth: 110,
            dicData: [
              {
                label: "是",
                value: true
              },
              {
                label: "否",
                value: false
              }
            ],
            rules: [
              {
                required: true,
                message: "默认学期不能为空",
                trigger: "blur"
              }
            ]
          }
        ]
      }
    };
  },
  created() {
    this.searchHandler();
  },
  methods: {
    searchHandler() {
      getTerm(this.searchForm).then(res => {
        this.page.pageSizes = res.data.pageSizes;
        this.page.total = res.data.total;
        this.tableData = res.data.records;
      });
    },
    onLoad(page) {
      this.searchForm.currentPage = page.currentPage;
      this.searchForm.pageSize = page.pageSize;
      this.searchHandler();
    },
    btnDelete(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          console.log(row);
          deleteTerm([row.id]).then(() => {
            this.$message.success("删除成功");
            this.searchHandler();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    rowUpdate(form, done, loading) {
      loading();
      updateTerm(form)
        .then(() => {
          this.$message.success("更新成功");
          this.searchHandler();
        })
        .finally(() => {
          done();
        });
    },
    rowSave(form, done, loading) {
      loading();
      console.log(form);
      addTerm(form)
        .then(() => {
          this.$message.success("添加成功");
          this.searchHandler();
        })
        .finally(() => {
          done();
        });
    }
  }
};
</script>
